#异常细分-用的不是很多
# import requests
# from requests import exceptions
# while True:
#     url = input("请输入要下载网页地址：")
#     try:
#         res = requests.get(url="scscnsc")
#         print(res)
#     except exceptions.MissingSchema as e:
#         print("URL架构不存在")
#     except exceptions.InvalidSchema as e:
#         print("URL架构错误")
#     except exceptions.InvalidURL as e:
#         print("URL地址格式错误")
#     except exceptions.ConnectionError as e:
#         print("网络连接错误")
#     except Exception as e:
#         print("代码出现错误", e)
#提示：如果想要写的简单一点，其实只写一个Exception捕获错误就可以了。
#自定义异常和抛出异常
# class MyClass(Exception):
#     pass
#
# try:
#     #。。。。
#     raise MyClass() #自定义的异常想要触发，就需要使用抛出异常
#     #。。。。
# except MyClass as e:
#     print("Myclass 异常被触发了",e)
# except Exception as e:
#     print("Exception",e)


# class MyException(Exception):
#     def __init__(self, msg, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.msg = msg
#
# try:
#     raise MyException("xxx失败了")
# except MyException as e:
#     print("MyException异常被触发了", e.msg)
# except Exception as e:
#     print("Exception", e)

# class MyException(Exception):
#     title = "请求错误"
#
# try:
#     raise MyException()
# except MyException as e:
#     print("MyException异常被触发了", e.title)
# except Exception as e:
#     print("Exception", e)

#案例：你我合作协同开发，你调用我写的方法
#1、我定义了一个函数
# import re
# class EmailValidError(Exception):
#     title="邮箱格式错误"
# class ContentRequiredError(Exception):
#     title="文本不能为空"
# def send_email(email,content):
#     if not re.match('\w+@live.com',email):
#         raise EmailValidError()
#     if len(content)==0:
#         raise ContentRequiredError()
# #2、调用
# def execute():
#     # 其他代码
#     # ...
#
#     try:
#         send_email("xiaofeng@live.com","")
#     except EmailValidError as e:
#         print(e.title)
#     except ContentRequiredError as e:
#         print(e.title)
#     except Exception as e:
#         print("发送失败")
#
# execute()

#特殊的finally
#在函数或方法中定义异常处理的代码时，要特别注意finally和return
# def func():
#     try:
#         a=123
#         return a
#     except Exception as e:
#         pass
#     finally:  #在try或exception中即使定义了return，也会执行最后的finally块中的代码
#         print(666)
# func()

# class IT(object):
#     def __init__(self):
#         self.count = 0
#     def __iter__(self):
#         return self
#     def __next__(self):
#         self.count += 1
#         if self.count == 3:
#             raise StopIteration
#         return self.count
# obj=IT()
# while True:
#     try:
#         i=next(obj)
#     except StopIteration as e:
#         print("数据获取完毕")
#         break
#     print(i)

#反射  getattr setattr hasattr delattr
#提供了一种更加灵活的方式让你可以实现去对象中操作成员（以字符串的形式去对象中进行成员的操作）
# class Person():
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#     def show(self):
#         print(self.name, self.age)

#
# obj=Person('John', 25)
#之前的方法
# print(obj.name)
# print(obj.age)
# obj.show()
# obj.age=21
# print(obj.age)


#反射

# print(getattr(obj,"name"))  获取成员
# print(getattr(obj,"age"))
#
# method=getattr(obj,"show")
# method()
# #上下等价
# getattr(obj,"show")()
#
# setattr(obj,"name","xiaofeng") 设置成员
# getattr(obj,"show")()

# v1=hasattr(obj,"name") #判断是否包含成员
# print(v1)
# getattr(obj,"show")()
# delattr(obj,"name")  #删除成员中的对象

#在python中，一切皆对象  对象、类、模块
#由于反射支持以字符的形式去对象中操作成员【等价与对象成员】，所以，基于反射也可以这样对类、模块中的成员进行操作
#小tip:只要看到xxx.ooo都可以使用反射实现

#import_module+反射
#在python中如果想要导入一个模块，可以通过import语法导入；企业也可以通过字符串的形式导入
#只能导入模块，不能导入模块中的成员
# from importlib import import_module
# m=import_module("random")
# m.randint(1,5)

